package com.sky.controller.admin;

import com.sky.constant.DishCacheConstant;
import com.sky.dto.SetmealDTO;
import com.sky.entity.Setmeal;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * SetmealController
 *
 * @author liliudong
 * @version 1.0
 * @description 套餐控制器
 * @date 2023/6/25 9:47
 */
@RestController
@RequestMapping("/admin/setmeal")
@Slf4j
@RequiredArgsConstructor
@Api(tags = "套餐管理")
public class SetmealController {

    private final SetmealService setmealService;

    /**
     * 新增套餐
     *
     * @param setmealDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增套餐")
    @CachePut(cacheNames = DishCacheConstant.CACHE_KEY, key = "#setmealDTO.categoryId")
    public Result<?> create(@RequestBody SetmealDTO setmealDTO) {
        setmealService.saveOne(setmealDTO);
        return Result.success();
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("批量删除")
    @CacheEvict(cacheNames = DishCacheConstant.CACHE_KEY, allEntries = true)
    public Result<?> deleteBatch(@RequestParam List<Long> ids) {
        setmealService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 修改套餐
     *
     * @param setmealDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改套餐")
    @CacheEvict(cacheNames = DishCacheConstant.CACHE_KEY, allEntries = true)
    public Result<?> update(@RequestBody SetmealDTO setmealDTO) {
        setmealService.update(setmealDTO);
        return Result.success();
    }

    /**
     * 起售停售
     *
     * @param status
     * @return
     */
    @ApiOperation("起售停售")
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = DishCacheConstant.CACHE_KEY, allEntries = true)
    public Result<?> updateStatus(@PathVariable Integer status, Long id) {
        setmealService.updateStatusById(id, status);
        return Result.success();
    }

    /**
     * 分页查询
     *
     * @return
     */
    @ApiOperation("分页查询")
    @GetMapping("/page")
    public Result<PageResult<Setmeal>> page(Long categoryId,
                                            String name,
                                            @RequestParam(defaultValue = "1") Long page,
                                            @RequestParam(defaultValue = "10") Long pageSize,
                                            Integer status) {
        PageResult<Setmeal> pageResult = setmealService.page(categoryId, name, status, page, pageSize);
        return Result.success(pageResult);
    }

    /**
     * 根据ID查询详情
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据ID查询详情")
    public Result<SetmealVO> getById(@PathVariable Long id) {
        SetmealVO setmealVO = setmealService.getById(id);
        return Result.success(setmealVO);
    }

}
